﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SoccerSimulator5000
{
    public partial class HomeForm : Form
    {

        public HomeForm()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            //Game.player.playerTeam.Draw(e.Graphics, 50, 20);
            DrawStandings(e.Graphics, 720, 100);
            DrawComingFixtures(e.Graphics, 100, 450);
        }

        private void DrawStandings( Graphics g, int x, int y )
        {
            System.Drawing.Font theFont = new System.Drawing.Font("Courier New", 14);
            g.FillRectangle(Brushes.Beige, x, y, 400, 300);
            g.DrawString(string.Format("{0,-29}{1,3:D}", "Team", "pts"), theFont, Brushes.Black, x + 2, y + 1);

            int standingsStart = Game.player.playerTeam.standingsData.position -1 - 2;
            int standingsStop = Game.player.playerTeam.standingsData.position -1  + 2;

            if (standingsStart < 0)
            {
                standingsStart = 0;
            }
            if (standingsStop > 11)
            {
                standingsStop = 11;
            }

            for (int i = standingsStart; i <= standingsStop; i++)
            {
                object[] arg = { Game.season.seasonStandings.standings[i].standingsData.position, Game.season.seasonStandings.standings[i].name, Game.season.seasonStandings.standings[i].standingsData.points };
                string team = string.Format("{0,-3}{1,-26}{2,3:D}", arg);
                if (i % 2 == 0)
                {
                    g.FillRectangle(Brushes.White, x, y + ((i + 1 - standingsStart) * 40), 400, 40);
                }
                g.DrawString(team, theFont, Brushes.Black, x + 2, y + (40 * (i +1 - standingsStart)) + 3);
            }
            //Game.player.playerTeam.CalculatePossibleEndPositions();
            //g.DrawString("Possible Positions: " + Game.player.playerTeam.bestEndPosition + "-" + Game.player.playerTeam.worstEndPosition, theFont, Brushes.Black, x + 2, y + (40 * 6) + 3);

        }

        private void DrawComingFixtures(Graphics g, int x, int y)
        {
            g.FillRectangle(Brushes.Beige, x, y, 750, 200);
            g.DrawString("Coming matches", new System.Drawing.Font("Courier New", 14), Brushes.Black, x + 2, y + 2);
            int startRound = Game.currentRound - 1;
            if ( startRound < 0 )
                startRound = 0;
            int j = 1;
            int endRound = startRound + 3;
            if (endRound > Game.season.seasonFixtures.fixtures.Count())
            {
                endRound = Game.season.seasonFixtures.fixtures.Count();
            }
            for (int i = startRound; i < endRound; i++)
            {
                foreach (var Match in Game.season.seasonFixtures.fixtures[i].round)
                {
                    if (Match.homeTeam.name == Game.player.playerTeam.name || Match.awayTeam.name == Game.player.playerTeam.name)
                    {
                        Match.Draw(x, y + (40 * j), g);
                        j++;
                    }
                }
            }
        }

        private void TeamForm_Activated(object sender, EventArgs e)
        {
            panel1.Invalidate();
        }

    }
}
